package com.changgou.goods.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.*;
import com.changgou.goods.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/****
 * @Author:传智播客
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * @Description 填写SKU信息（商品）
     * @ClassName CategoryController
     * @date 2019/11/13 18:27
     * @return
     */
    @PostMapping("/save")
    public Result saveUpdate(@RequestBody Goods goods){
        categoryService.saveUpdate(goods);
        return new Result(true,StatusCode.OK,"保存成功");
    }

    /**
     * @Description 根据categoryId查询Para
     * @ClassName CategoryController
     * @date 2019/11/13 19:35
     * @return
     */
    @GetMapping("/findCategoryByPara/{id}")
    public Result findCategoryByPara(@PathVariable(value = "id")Integer id){
        Para para =  categoryService.findCategoryByPara(id);
        return new Result(true,StatusCode.OK,"参数查询成功",para);
    }

    /**
     * @Description  查询规格
     * @ClassName CategoryController
     * @date 2019/11/13 19:23
     * @return
    */
    @GetMapping("/findByCategoryId/{id}")
    public Result<List<Spec>> findByCategoryId(@PathVariable(value = "id") Integer id){
        List<Spec> list  = categoryService.findByCategoryId(id);
        return new Result<>(true,StatusCode.OK,"规格查询成功",list);
    }





    /**
     *      查询模板
     * @param id
     * @return
     */
    @GetMapping("/findCategoryTemplate/{id}")
    public Result fndCategoryTemplate(@PathVariable(value = "id")Integer id){
        Template template = categoryService.findCategoryTemplate(id);
        return new Result<>(true,StatusCode.OK,"模板查询成功",template);
    }

    /**
     *      查询商品
     * @param categoryId
     * @return
     */
    @GetMapping("/findCategoryBrandById/{categoryId}")
    public Result<List<Brand>> findCategoryBrandById(@PathVariable(value = "categoryId") Integer categoryId){
        List<Brand> list =  categoryService.findCategoryBrandById(categoryId);
        return new Result<>(true,StatusCode.OK,"查询商品成功",list);
    }


    /**
     * 查询分类分类
     * @param parentId
     * @return
     */
    @GetMapping("/findCateGoryParentId/{parentId}")
    public Result<List<Category>> findCateGoryParentId(@PathVariable(value = "parentId") Integer parentId ){
        List<Category> list = categoryService.findCateGoryParentId(parentId);
        return new Result<>(true,StatusCode.OK,"查询分类完成",list);
    }

    /***
     * Category分页条件搜索实现
     * @param category
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Category category, @PathVariable  int page, @PathVariable  int size){
        //调用CategoryService实现分页条件查询Category
        PageInfo<Category> pageInfo = categoryService.findPage(category, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Category分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用CategoryService实现分页查询Category
        PageInfo<Category> pageInfo = categoryService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param category
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Category>> findList(@RequestBody(required = false)  Category category){
        //调用CategoryService实现条件查询Category
        List<Category> list = categoryService.findList(category);
        return new Result<List<Category>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用CategoryService实现根据主键删除
        categoryService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Category数据
     * @param category
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Category category,@PathVariable Integer id){
        //设置主键值
        category.setId(id);
        //调用CategoryService实现修改Category
        categoryService.update(category);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Category数据
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Category category){
        //调用CategoryService实现添加Category
        categoryService.add(category);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Category数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable Integer id){
        //调用CategoryService实现根据主键查询Category
        Category category = categoryService.findById(id);
        return new Result<Category>(true,StatusCode.OK,"查询成功",category);
    }

    /***
     * 查询Category全部数据
     * @return
     */
    @GetMapping
    public Result<List<Category>> findAll(){
        //调用CategoryService实现查询所有Category
        List<Category> list = categoryService.findAll();
        return new Result<List<Category>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
